<?php
/**
 *description:Auto
 *author:李飞
 *time:2016/12/29
 */

namespace Admin\Controller;
use Think\Controller;
class AutoeditController extends Controller {

	//每日库存统计
	public function goods_log(){
		C("DB_DEPOT.DB_TYPE","mysql");
		C("DB_DEPOT.DB_HOST","localhost");
		// C("DB_DEPOT.DB_NAME","hcf");
		C("DB_DEPOT.DB_NAME","tzwms_18020170001");
		C("DB_DEPOT.DB_USER","root");
		// C("DB_DEPOT.DB_PWD","root");
		C("DB_DEPOT.DB_PWD","8212dd1c4e");//250服务器
		// C("DB_DEPOT.DB_PWD","3fcb0c61d3");//136服务器
		C("DB_DEPOT.DB_PORT","3306");
		C("DB_PRS","tz_");
		C("DB_CHARSET","utf8");
		
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        $m=M('Goods',C('DB_PRS'),C('DB_DEPOT'));
		$gl=M('GoodsLog',C('DB_PRS'),C('DB_DEPOT'));
		$bc=M('BooksCate',C('DB_PRS'),C('DB_DEPOT'));
		
		$p=M('Packing',C('DB_PRS'),C('DB_DEPOT'));
		$s=M('StorageOutbound',C('DB_PRS'),C('DB_DEPOT'));
		$ss=M('StatisticalStorage',C('DB_PRS'),C('DB_DEPOT'));
		
		$po=M('Pushorder',C('DB_PRS'),C('DB_DEPOT'));
		$pog=M('Pushordergoods',C('DB_PRS'),C('DB_DEPOT'));
		
		// $time=time()-3600*24*4;//两天前
		for($i=25;$i<115;$i++){
		$time=time()-3600*24*$i;//当前时间
		$time_today=date("Y-m-d",$time);//今天
		$time_nextday=date("Y-m-d",$time+3600*24);//明天
		
		$str_time_today=strtotime($time_today);//今天(时间戳)
		$str_time_nextday=strtotime($time_nextday);//明天(时间戳)
		//当日出库数量-begin
		$map_po=array();
		$map_po['_string']="po.delivery_time>$str_time_today and po.delivery_time<$str_time_nextday";
		$goods_list=$po->alias('po')->join('__PUSHORDERGOODS__ g on po.id=g.order_id')->where($map_po)->group('g.goods_no')->order('po.id')->getField('g.goods_no,g.goods_name,sum(g.goods_nums) sum_goodsnums');
		//当日出库数量-end
		
		//当日接收数量-begin
		$map_ps=array();
		$map_ps['_string']="po.add_time>$str_time_today and po.add_time<$str_time_nextday";
		$goods_list_ps=$po->alias('po')->join('__PUSHORDERGOODS__ g on po.id=g.order_id')->where($map_ps)->group('g.goods_no')->order('po.id')->getField('g.goods_no,g.goods_name,sum(g.goods_nums) sum_goodsnums');
		//当日接收数量-end
		// echo $po->_sql();
		// dump($goods_list_ps);
		// die;
		//当日打包数量-begin
		$map_pack=array();
		$map_pack['_string']="DATE_FORMAT(p.created_at,'%Y-%m-%d')='$time_today'";
		$goods_list_pack=$p->alias('p')->join('__PUSHORDER__ po on p.order_no=po.order_no')->join('__PUSHORDERGOODS__ g on po.id=g.order_id')->where($map_pack)->group('g.goods_no')->order('po.id')->getField('g.goods_no,g.goods_name,sum(g.goods_nums) sum_goodsnums');
		//当日打包数量-end
		
		//当日称重数量-begin
		$map_weight=array();
		$map_weight['_string']="DATE_FORMAT(p.created_at,'%Y-%m-%d')='$time_today'";
		$map_weight['pack_weight']=array('exp','is not null');
		$goods_list_weight=$p->alias('p')->join('__PUSHORDER__ po on p.order_no=po.order_no')->join('__PUSHORDERGOODS__ g on po.id=g.order_id')->where($map_weight)->group('g.goods_no')->order('po.id')->getField('g.goods_no,g.goods_name,sum(g.goods_nums) sum_goodsnums');
		//当日称重数量-end
		
		//当日导单数量-begin
		$url_inbound=C('ERP_URL')."/index.php/Apiedit/inbound_nums";//250服务器
		$data_inbound=array();
		$data_inbound['time']=$time;
		$data_inbound['warehouse_code']='11330213';
		$data_inbound['manua_id']=$bc->where(array('status'=>1))->getField('code',true);
		$inbound_res=push($url_inbound,$data_inbound);
		$inbound_res=json_decode($inbound_res,true);
		$goods_list_inbound=$inbound_res['message'];
		//当日导单数量-end
		
		//当日删单数量-begin
		$data_inbound['delete']=1;
		$del_res=push($url_inbound,$data_inbound);
		$del_res=json_decode($del_res,true);
		$goods_list_delete=$del_res['message'];
		//当日删单数量-end									   
		// dump($ao);
        //搜索条件
        $map=array();  
		$map['m.status']=1;
		// $map['a.storage_amount']=array('GT',0);
		$info=$m->alias('m')->field('m.*,count(a.freez_amount) info_count,sum(a.freez_amount) freez_amount,sum(a.storage_amount) storage_amount')->join("__ARRIVAL_ORDER__ a on m.goods_no=a.goods_no and m.manua_id=a.manual_id","left")->group('m.goods_no,m.manua_id')->order("manua_id,goods_no")->select();
		$arr=array();
		
		$time=strtotime(date("Y-m-d",$time));
		$where_string="UNIX_TIMESTAMP(FROM_UNIXTIME(add_time,'%Y-%m-%d'))=$time";
		
		foreach($info as $k=>$v){
			$arr[$k]['oubound_nums']=$goods_list[$v['goods_no']]['sum_goodsnums'];//当日出库数量
			$arr[$k]['receive_nums']=$goods_list_ps[$v['goods_no']]['sum_goodsnums'];//当日接收数量
			$arr[$k]['pack_nums']=$goods_list_pack[$v['goods_no']]['sum_goodsnums'];//当日打包数量
			$arr[$k]['weight_nums']=$goods_list_weight[$v['goods_no']]['sum_goodsnums'];//当日称重数量
			$arr[$k]['inbound_nums']=$goods_list_inbound[$v['goods_no']]['sum_goodsnums'];//当日导单数量
			$arr[$k]['delete_order_nums']=$goods_list_delete[$v['goods_no']]['sum_goodsnums'];//当日删单数量
			// $arr[$k]['freez_nums']=$v['freez_amount'];//剩余冻结数量

			$map_gl=array();
			$map_gl['goods_no']=$v['goods_no'];
				
			$res=$gl->where($where_string)->where($map_gl)->save($arr[$k]);
			// dump($res);
			// dump($v['goods_no']);
			// die;
		}
		}
		
    }
}
